package com.wuchao.controller;

import com.mysql.cj.util.StringUtils;
import com.wuchao.entity.User;
import com.wuchao.service.serviceImpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2022/7/12 10:55
 * @Version 1.0
 */
@WebServlet(name = "ServletPage",urlPatterns = "/ServletPage")
public class ServletPage extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//乱码问题
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            //out对象
            PrintWriter out = response.getWriter();//文件流
            //刷新缓存区 关闭
            out.flush();
            out.close();
        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //乱码问题
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            //out对象
            PrintWriter out = response.getWriter();//文件流
            ServiceImpl service = new ServiceImpl();
            //默认第一页，然后每页展示数 2
           // StringUtils.isNullOrEmpty();
            int currentPage=1;//当前页
            int pageSize=2;//每页展示数
            int totals=service.isLoading("select count(1) from user");//总条数
            int pages=totals%pageSize==0?totals/pageSize:totals/pageSize+1;//总页数
            if(!StringUtils.isNullOrEmpty(request.getParameter("currentPage"))){
                currentPage=Integer.parseInt(request.getParameter("currentPage"));
            } if(!StringUtils.isNullOrEmpty(request.getParameter("pageSize"))){
                pageSize=Integer.parseInt(request.getParameter("pageSize"));
            }
            List<User> poJo = service.findPoJo("select * from user limit ?,?", (currentPage - 1) * pageSize, pageSize);
            request.setAttribute("totals",totals);
            request.setAttribute("currentPage",currentPage);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pages",pages);
            request.setAttribute("list",poJo);
            request.getRequestDispatcher("/jstlnc/page.jsp").forward(request,response);
            out.flush();
            out.close();

        }
}
